package com.sinoeyes.controller.system;

import java.io.IOException;

import com.sinoeyes.auth.core.LoginUser;
import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.auth.service.TokenService;
import com.sinoeyes.common.config.PerformanceConfig;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.common.utils.ServletUtils;
import com.sinoeyes.common.utils.file.FileUploadUtils;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.service.ISysUserService;
import com.sinoeyes.system.vo.SysUserVo;
import com.sinoeyes.vo.system.UserProfileVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 个人信息 业务处理
 *
 */
@RestController
@RequestMapping("/system/user/profile")
@Api(value = "个人信息 业务处理 API", tags = {"个人信息"})
public class SysProfileController extends BaseController
{
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 个人信息
     */
    @GetMapping
    @ApiOperation("个人信息")
    public Result<UserProfileVo> profile() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        UserProfileVo userProfileVo = new UserProfileVo();
        SysUserVo user = loginUser.getUser();
        userProfileVo.setUserVo(user);
        userProfileVo.setRoleGroup(userService.selectUserRoleGroup(loginUser.getUsername()));
        userProfileVo.setPostGroup(userService.selectUserPostGroup(loginUser.getUsername()));
        return Result.success(userProfileVo);
    }

    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改用户")
    public Result<Integer> updateProfile(@RequestBody SysUserVo user)
    {
        return Result.success(userService.updateUserProfile(user));
    }

    /**
     * 重置密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePwd")
    @ApiOperation("重置密码")
    public Result<Integer> updatePwd(String oldPassword, String newPassword)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String userName = loginUser.getUsername();
        String password = loginUser.getPassword();
        if (!SecurityUtils.matchesPassword(oldPassword, password))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改密码失败，旧密码错误");
        }
        if (SecurityUtils.matchesPassword(newPassword, password))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新密码不能与旧密码相同");
        }
        return Result.success(userService.resetUserPwd(userName, SecurityUtils.encryptPassword(newPassword)));
    }

    /**
     * 头像上传
     */
    @Log(title = "用户头像", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    @ApiOperation("头像上传")
    public Result<String> avatar(@RequestParam("avatarfile") MultipartFile file) throws IOException
    {
        if (!file.isEmpty())
        {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            String avatar = FileUploadUtils.upload(PerformanceConfig.getAvatarPath(), file);
            if (userService.updateUserAvatar(loginUser.getUsername(), avatar)) {
                return Result.success(avatar);
            }
        }
        return Result.failure(RCode.FAILURE.getCode(), "上传图片异常，请联系管理员");
    }
}
